package com.study.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.annotation.AuthCheck;
import com.study.common.BaseResponse;
import com.study.common.DeleteRequest;
import com.study.common.ResultUtils;
import com.study.constant.UserConstant;
import com.study.exception.ErrorCode;
import com.study.exception.ThrowUtils;
import com.study.model.dto.post.PostAddRequest;
import com.study.model.dto.post.PostQueryRequest;
import com.study.model.dto.post.PostUpdateRequest;
import com.study.model.dto.topic.TopicAddRequest;
import com.study.model.dto.topic.TopicQueryRequest;
import com.study.model.dto.topic.TopicUpdateRequest;
import com.study.model.entity.Post;
import com.study.model.entity.Topic;
import com.study.model.vo.PostVO;
import com.study.model.vo.TopicVO;
import com.study.service.PostService;
import com.study.service.TopicService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 帖子控制器
 */
@RestController
@RequestMapping("/post")
public class PostController {



    @Resource
    private PostService postService;

    /**
     * 创建帖子
     */
    @PostMapping("/add")
    public BaseResponse<Long> addPost(@RequestBody PostAddRequest postAddRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(postAddRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(postService.createPost(postAddRequest,request));
    }

    /**
     * 根据 id 获取帖子（仅管理员）
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Post> getPostById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        Post post = postService.getById(id);
        ThrowUtils.throwIf(post == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(post);
    }

    /**
     * 根据 id 获取包装类
     */
    @GetMapping("/get/vo")
    public BaseResponse<PostVO> getPostVOById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        PostVO postVO=postService.getPostVOById(id);
        return ResultUtils.success(postVO);
    }


    /**
     * 删除帖子
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deletePost(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(postService.romovePost(deleteRequest.getId()));

    }

    /**
     * 更新帖子
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updatePost(@RequestBody PostUpdateRequest postUpdateRequest) {
        ThrowUtils.throwIf(postUpdateRequest == null || postUpdateRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(postService.updatePost(postUpdateRequest));

    }

    /**
     * 分页获取帖子封装列表（仅管理员）
     *
     * @param postQueryRequest 查询请求参数
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<PostVO>> listPostVOByPage(@RequestBody PostQueryRequest postQueryRequest) {
        ThrowUtils.throwIf(postQueryRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(postService.listPostVOByPage(postQueryRequest));
    }

}
